package com.lemurian.bankdata.utils;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.apache.struts2.util.ServletContextAware;

import com.lemurian.bankdata.lite.utils.GeneralConstant;
import com.opensymphony.xwork2.ActionSupport;

public abstract class BaseAction extends ActionSupport implements SessionAware, ServletContextAware, ServletResponseAware, ServletRequestAware {
	private Map<String, Object> session;
    private ServletContext servletContext;
    private HttpServletResponse response;
    private HttpServletRequest request;
    private static final long serialVersionUID = 1L;
    int start = 0, count = 20;
    String previousPage, ascDesc = "asc", orderBy = "", pager = "", pagerItem = "", search = "", subaction = "";
    int orderId=0;
    private boolean readOnly = false;
    private String mappedRequest;

    private String paramObjectId;

    private String modelMethod;
    private String actionPrefix;
    private boolean onHome = false;

    public BaseAction() {

    }

    public String getPreviousPage() {
        return previousPage;
    }

    public void setPreviousPage(String previousPage) {
        this.previousPage = previousPage;
    }

    @SkipValidation
    public abstract String list();

    /*@SkipValidation
    public abstract String partialList();*/
    @SkipValidation
    public abstract String add();

    public abstract String save();

    @SkipValidation
    public abstract String edit();

    public abstract String update();

    @SkipValidation
    public abstract String detail();

    @SkipValidation
    public abstract String delete();

    public String getActionClass() {
        if(getActionPrefix()!=null && !getActionPrefix().isEmpty())
            return getActionPrefix();
        else
            return getClass().getSimpleName();
    }

    public String getDestination() {
        return getClass().getSimpleName();
    }

    public String getActionMethod() {
        return mappedRequest != null ? (mappedRequest.indexOf(".doku") > -1 ? (mappedRequest.substring(0, mappedRequest.indexOf(".doku"))) : mappedRequest) : mappedRequest;
    }

    // when invalid, the request parameter will restore command action
    public void setActionMethod(String method) {
        this.mappedRequest = method;
    }

    // this prepares command for button on initial screen write
    public void setMappedRequest(String actionMethod) {
        this.mappedRequest = getActionClass() + "_" + actionMethod + ".doku";
    }

    public void setMappedRequest(String actionMethod, String param) {
        this.mappedRequest = getActionClass() + "_" + actionMethod + ".doku" + param;
    }

    public void setMappedRequestFullPath(String actionMethod) {
        this.mappedRequest = actionMethod;
    }

    public String getMappedRequest() {
        return mappedRequest;
    }

    public void setReadOnly(boolean readOnly) {
        this.readOnly = readOnly;
    }

    public boolean isReadOnly() {
        return readOnly;
    }

    public int getCount() {
        return count;
    }

    public int getStart() {
        return start;
    }

    public void setCount(int count) {
        this.count = count;

    }

    public void setStart(int start) {
        this.start = start;
    }

    public String getAscDesc() {
        return ascDesc;
    }

    public void setAscDesc(String ascDesc) {
        this.ascDesc = ascDesc;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    public String getPager() {
        return pager;
    }

    public void setPager(String pager) {
        this.pager = pager;
    }

    public String getPagerItem() {
        return pagerItem;
    }

    public void setPagerItem(String pagerItem) {
        this.pagerItem = pagerItem;
    }

    public String getSearch() {
        return search;
    }

    public void setSearch(String search) {
        this.search = search;
    }

    public String getSubaction() {
        return subaction;
    }

    public void setSubaction(String subaction) {
        this.subaction = subaction;
    }

    public String getParamObjectId() {
        return paramObjectId;
    }

    public void setParamObjectId(String paramObjectId) {
        this.paramObjectId = paramObjectId;
    }

    public int getOrderId() {
        return orderId;
    }

    public void setOrderId(int orderId) {
        this.orderId = orderId;
    }


    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    public void setSession(Map<String, Object> session) {
        this.session = session;
    }

    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    public void setServletResponse(HttpServletResponse response) {
        this.response = response;
    }

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public ServletContext getServletContext() {
        return servletContext;
    }

    public Map<String, Object> getSession() {
        return session;
    }

    public String getModelMethod() {
        return modelMethod;
    }

    public void setModelMethod(String modelMethod) {
        this.modelMethod = modelMethod;
    }

    public String getActionPrefix() {
        return actionPrefix;
    }

    public void setActionPrefix(String actionPrefix) {
        this.actionPrefix = actionPrefix;
    }

    public boolean isOnHome() {
        return onHome;
    }

    public void setOnHome(boolean onHome) {
        this.onHome = onHome;
    }

    public String getCustomActionMessage() {
        String msg = (String) getSession().get(GeneralConstant.CUSTOM_ACTION_MESSAGE);
        getSession().remove(GeneralConstant.CUSTOM_ACTION_MESSAGE);
        return msg;
    }

    public void addCustomActionMessage(String message) {
        getSession().put(GeneralConstant.CUSTOM_ACTION_MESSAGE, message);
    }

    public boolean hasCustomActionWarning() {
        String msg = (String) getSession().get(GeneralConstant.CUSTOM_ACTION_WARNING);
        if(msg!=null && msg instanceof String && msg.length()>0)
            return true;
        return false;
    }
    
    public String getCustomActionWarning() {
        String msg = (String) getSession().get(GeneralConstant.CUSTOM_ACTION_WARNING);
        getSession().remove(GeneralConstant.CUSTOM_ACTION_WARNING);
        return msg;
    }

    public void addCustomActionWarning(String message) {
        getSession().put(GeneralConstant.CUSTOM_ACTION_WARNING, message);
    }

    public boolean hasCustomActionMessage() {
        String msg = (String) getSession().get(GeneralConstant.CUSTOM_ACTION_MESSAGE);
        if(msg!=null && msg instanceof String && msg.length()>0)
            return true;
        return false;
    }

    public String getCustomActionError() {
        String msg = (String) getSession().get(GeneralConstant.CUSTOM_ACTION_ERROR);
        getSession().remove(GeneralConstant.CUSTOM_ACTION_ERROR);
        return msg;
    }

    public void addCustomActionError(String message) {
        getSession().put(GeneralConstant.CUSTOM_ACTION_ERROR, message);
    }

    public boolean hasCustomActionError() {
        String msg = (String) getSession().get(GeneralConstant.CUSTOM_ACTION_ERROR);
        if(msg!=null && msg instanceof String && msg.length()>0)
            return true;
        return false;
    }
}